import numpy as np
from datetime import datetime, timedelta

class shapeOfLine(object):

    """
    一些自定义函数
    """

    def __init__(self):
        pass

    # 检测某个点在未来是否会下降或上升, ascent = 1 判断上升, ascent = -1， 判断下降
    def isMonotony_infuture(arr, target, ascent, span = 5):
        l = len(arr)
        if target + span >= l: return False

        for i in range(target + 1, target + span, 1):
            if (arr[i] - arr[i - 1]) * ascent <= 0: return False
        return True

    # 检测某个点在过去是否会下降或上升, ascent = 1 判断上升, ascent = -1， 判断下降
    def isMonotony_inpast(arr, target, ascent, span = 5):
        #l = len(arr)
        if target - span < 0: return False

        for i in range(target - span + 1, target + 1, 1):
            if (arr[i] - arr[i - 1]) * ascent <= 0: return False
        return True

    # 检测某个点在arr中是否为波谷
    def isValley(arr, target, span = 5):
        l = len(arr)
        if target - span < 0 or target + span >= l:
            return False

        for i in range(target - span + 1, target + 1, 1):
            if arr[i - 1] <= arr[i]: return False
        for i in range(target, target + span - 1, 1):
            if arr[i + 1] <= arr[i]: return False
        return True

    # 检测某个点在arr中是否为峰值处
    def isTop(arr, target, span = 5):
        l = len(arr)
        if target - span < 0 or target + span >= l:
            return False

        for i in range(target - span + 1, target + 1, 1):
            if arr[i - 1] >= arr[i]: return False
        for i in range(target, target + span - 1, 1):
            if arr[i + 1] >= arr[i]: return False
        return True

    #检测某个点是否是死叉
    def isDeadCross(MACD, DEA, target):
        if target >= len(DEA): return False
        return target >= 1 and MACD[target] < DEA[target] and MACD[target - 1] >= DEA[target - 1]

    def average(self, data):
        sum = 0;
        for i in range(len(data)):
            sum += data[i]
        return sum / len(data)

    # 线性回归函数
    def linear_regression(self, arr):
        points = np.column_stack((np.arange(len(arr)), arr))
        #print(points)
        xbar = self.average(points[:, 0])

        sum_yx = 0
        sum_x2 = 0
        sum_delta = 0
        M = len(arr)
        for i in range(M):
            x = points[i, 0]
            y = points[i, 1]
            sum_yx += y * (x - xbar)
            sum_x2 += x ** 2

        k = sum_yx / (sum_x2 - M * (xbar ** 2))

        for i in range(M):
            x = points[i, 0]
            y = points[i, 1]
            sum_delta += (y - k * x)

        b = sum_delta / M
        return k, b

#将时间加上delta天
def timecal(date_str, delta):
    #date_obj = datetime.strptime(date_str, '%Y-%m-%d').date()
    new_date = date_str + timedelta(days=delta)
    #new_date_str = new_date.strftime('%Y-%m-%d')
    return new_date

# 计算时间差
def time_delta(time1, time2): #time1 - time2, 'yyyymmdd'
    # 解析日期字符串为 datetime 对象
    date1 = datetime.strptime(time1, "%Y%m%d")
    date2 = datetime.strptime(time2, "%Y%m%d")

    # 计算时间差
    time_diff = date2 - date1

    # 返回时间差对象
    return time_diff.days